package com.school.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.school.model.Food;
import com.school.model.User;
import com.school.service.impl.UserServiceImpl;
import com.school.utils.CartManager;

public class UserAction 
{

    UserServiceImpl userService = new UserServiceImpl();

    // �û�ע��
    public String addUser()
    {
        HttpServletRequest request = ServletActionContext.getRequest();

        String uLoginId = request.getParameter("uLoginId");
        String userPass = request.getParameter("userPass");
        String userName = request.getParameter("userName");
        String uAddress = request.getParameter("uAddress");
        String uEmail = request.getParameter("uEmail");
        String uPhone = request.getParameter("uPhone");

        int result = userService.userRegister(uLoginId, userPass, userName,
                uAddress, uEmail, uPhone);

        if (result < 0)
        {
            return "error";
        }
        else
        {
            return "success";
        }
    }

    // �û���¼
    public String userLogin()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String uLoginId = request.getParameter("uLoginId");
        String userPass = request.getParameter("userPass");
        User user = userService.userLogin(uLoginId, userPass);
        if (user != null)
        {
            HttpSession session = request.getSession();
            session.setAttribute("User", user);
            return "userIndex";
        }
        return "login";
    }

    // ��ʾ���в�Ʒ
    public String displayFoods()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        List<Food> foods = userService.displayFoods();
        if (foods == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("Foods", foods);
            return "allFoods";
        }
    }

    // ��Ʒ��ϸ��Ϣ
    public String foodDetails()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("foodId");
        int foodId = Integer.parseInt(id);
        Food food = userService.foodDetails(foodId);
        if (food == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("Food", food);
            return "foodDetails";
        }
    }

    // �޸��˻�
    public String alterUserDetail()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String uLoginId = request.getParameter("uLoginId");
        String userName = request.getParameter("userName");
        String userPass = request.getParameter("userPass");
        String uAddress = request.getParameter("uAddress");
        String uPhone = request.getParameter("uPhone");
        String uEmail = request.getParameter("uEmail");
        String id = request.getParameter("userId");
        int userId = Integer.parseInt(id);

        int result = userService.alterUserDetail(uLoginId, userName, userPass,
                uAddress, uPhone, uEmail, userId);

        if (result < 0)
        {
            return "error";
        }
        else
        {
            return "success";
        }
    }

    // ��ʾָ����Ʒ
    public String displaySelFoods()
    {
        HttpServletRequest request = ServletActionContext.getRequest();

        String cid = request.getParameter("cuisineId");
        String did = request.getParameter("districtId");
        if (cid == null)
        {
            int districtId = Integer.parseInt(did);
            List<Food> foods = userService.displaySelDisFoods(districtId);
            if (foods == null)
            {
                return "error";
            }
            else
            {
                Food food = foods.get(0);
                request.setAttribute("SDFood", food);
                request.setAttribute("SDFoods", foods);
                return "selectedDisFoods";
            }
        }
        else if (did == null)
        {
            int cuisineId = Integer.parseInt(cid);
            List<Food> foods = userService.displaySelCuiFoods(cuisineId);

            if (foods == null)
            {
                return "error";
            }
            else
            {
                Food food = foods.get(0);
                request.setAttribute("SCFood", food);
                request.setAttribute("SCFoods", foods);
                return "selectedCuiFoods";
            }
        }

        else
        {
            return "error";
        }
    }

    // ���в�Ʒ���
    public String allIntro()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        List<Food> foods = userService.allIntro();
        if (foods == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("Foods", foods);
            return "all";
        }
    }

    // ������Ʒ���
    public String singleIntro()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("foodId");
        int foodId = Integer.parseInt(id);

        List<Food> foods = userService.singleIntro(foodId);
        if (foods == null)
        {
            return "error";
        }
        else
        {
            Food food = foods.get(0);
            request.setAttribute("Food", food);
            request.setAttribute("SIFoods", foods);
            return "single";
        }
    }

    // �û�����
    public String userComment()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String uid = request.getParameter("userId");
        int userId = Integer.parseInt(uid);
        String eid = request.getParameter("eateryId");
        int eateryId = Integer.parseInt(eid);
        String content = request.getParameter("content");

        int result = userService.userComment(content, eateryId, userId);

        if (result < 0)
        {
            return "error";
        }
        else
        {
            return "success";
        }
    }
    
 // ����ָ����Ʒ
    public String serchFoods() throws UnsupportedEncodingException
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String name = request.getParameter("foodName");
        String foodName = new String(name.getBytes("iso-8859-1"),"utf-8");
        List<Food> foods = userService.serchFoods(foodName);
        if (foods == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("Foods", foods);
            return "allFoods";
        }
    }
    
    public String addCart()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("foodId");
        int foodId = Integer.parseInt(id);
        String quan = request.getParameter("quantity");
        
        
        //�������ﳵ����
        CartManager cartManager = new CartManager();
        
        //��ȡsession
        HttpSession session = request.getSession();
        
        //�õ����ﳵ����
        ArrayList cart = (ArrayList)session.getAttribute("cart");
        
        cartManager.setCart(cart);
        
        Food food = userService.displayCart(foodId);
        
        if (food==null)
        {
            return "error";
        }
        else
        {
            if (quan == null)
            {
                cartManager.addToCart(food, 1);
            }
            else
            {
                int quantity = Integer.parseInt(quan);
                cartManager.addToCart(food, quantity);
            }
            session.setAttribute("cart", cartManager.getCart());
            return "cart";
        }
        
    }

}
